package com.weds.data_sync.event.listener;

import com.weds.data_sync.event.ExecutorPlanErrorEvent;
import com.weds.data_sync.event.ExecutorPlanFinishEvent;
import com.weds.data_sync.mapper.DataSyncPlanRecordMapper;
import com.weds.data_sync.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;

/**
 * @Description 执行计划事件监听
 * @Author lihw
 * @Date 2024/1/3 11:36
 */
@Slf4j
@Component
public class ExecutorPlanListener {

    @Autowired
    private DataSyncPlanRecordMapper planRecordMapper;

    @Autowired
    private RecordService recordService;

    /**
     * 执行计划执行完毕
     *
     * @param event
     */
    @Async
    @EventListener
    public void onPlanExecutorFinishEvent(@NotNull ExecutorPlanFinishEvent event) {
        log.info("Received ExecutorPlanFinishEvent event: {}", event);

        /**
         * 寻找执行计划队列中同类型的执行计划，并执行
         * 如果没有，则不操作
         * 然后，阅读同步记录，发布广播
         */

        // 将新增的数据同步到各个业务接口
        recordService.analysePlanRecord();

    }

    @Async
    @EventListener
    public void onPlanExecutorErrorEvent(@NotNull ExecutorPlanErrorEvent event) {
        log.info("onPlanExecutorErrorEvent 执行， dto:{}, idx: {}, msg: {}", event.getExecutePlan(), event.getErrorIndex(), event.getErrorMsg());
        /**
         * 执行计划执行出错，托底机制
         */
    }


}
